package com.westa.smallshop.goodsadd;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.data.source.GoodsItemDataSource;
import com.westa.smallshop.data.source.ImageSource;
import com.westa.smallshop.data.source.remote.BaseDataSource;
import com.westa.smallshop.util.ListUtils;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;


class GoodsAddPresenter implements GoodsAddContract.Presenter {
    @NonNull
    private final GoodsAddContract.View mView;

    @NonNull
    private final GoodsItemDataSource mGoodsItemDataSource;

    @NonNull
    private final ImageSource mImageSource;

    GoodsAddPresenter(@NonNull GoodsAddContract.View view,
                      @NonNull GoodsItemDataSource goodsItemDataSource,
                      @NonNull ImageSource imageSource) {
        mView = checkNotNull(view);
        mGoodsItemDataSource = checkNotNull(goodsItemDataSource);
        mImageSource = checkNotNull(imageSource);
        mView.setPresenter(this);

    }

    @Override
    public void start() {
    }

    @Override
    public void doSaveGoods(final List<String> images, final GoodsItem goodsItem) {

        if (TextUtils.isEmpty(goodsItem.getGoods_name())) {
            mView.showGoodsNameEmpty();
            return;
        }
        if (TextUtils.isEmpty(goodsItem.getGoods_desc())) {
            mView.showGoodsDescriptionNull();
            return;
        }
        if (TextUtils.isEmpty(String.valueOf(goodsItem.getGoods_price()))) {
            mView.showGoodsPriceEmpty();
            return;
        }
        mView.showAddGoodsDialogProgress();

        if (ListUtils.isEmpty(images)) {
            doAddGoodsItem(goodsItem, images);
        } else {

            mImageSource.upLoadImages(images, new BaseDataSource.LoadCallBack<List<String>>() {
                @Override
                public void onLoadDataSuccessful(List<String> data) {
                    doAddGoodsItem(goodsItem, data);
                }

                @Override
                public void onLoadDataFailed() {
                    mView.dismissDialogProgress();
                    mView.showUploadImageFailed();
                    mImageSource.cancelLoadRequest();
                }
            });
        }
    }


    public void doAddGoodsItem(GoodsItem goodsItem, List<String> images) {


        mGoodsItemDataSource.uploadGoods(goodsItem, images.get(0), new BaseDataSource.LoadCallBack<JSONObject>() {
            @Override
            public void onLoadDataSuccessful(JSONObject data) {
                if (mView.isActive()) {
                    if (TextUtils.equals(data.getString("result"), "0")) {
                        mView.dismissDialogProgress();
                        mView.showAddGoodsSuccessful();
                    } else {
                        mView.showAddGoodsFailed();
                        mView.dismissDialogProgress();
                    }
                }
            }

            @Override
            public void onLoadDataFailed() {
                cancelAddGoodsImage();
                mView.dismissDialogProgress();
                mView.showAddGoodsFailed();
            }
        });

    }

    @Override
    public void cancelAddGoodsImage() {
        mImageSource.cancelLoadRequest();
    }


    @Override
    public void destroy() {
        mGoodsItemDataSource.cancelLoadRequest();
    }
}
